package com.rankeiot.platform.util;


public class DefaultTokenManager implements TokenManager {
    private Token token;
    private final Object lock = new Object();
    private TokenHandler getToken;

    public DefaultTokenManager(TokenHandler tokenHandler) {
        this.getToken = tokenHandler;
        token=new Token();

    }

    @Override
    final public String getToken() {
        if (this.isNeedRefresh()) {
            synchronized (this.lock) {
                if (this.isNeedRefresh()) {
                    getToken.updateToken(token);
                }
            }
        }
        return this.token.getToken();
    }

    /**
     * 获取新的Token
     *
     * @return
     */
    // protected abstract String getNewToken();
    protected boolean isNeedRefresh() {
        long now = System.currentTimeMillis();
        return now > token.expirationTime;
    }
}
